
package cn.zktr.Personnel.controllers.tantiantian;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerTngPlan;
import cn.zktr.Personnel.model.pojos.PerTngPojs;
import cn.zktr.Personnel.model.pojos.PerTngRet;
import cn.zktr.Personnel.model.pojos.PerTngRus;
import cn.zktr.Personnel.model.pojos.PerTngXes;
import cn.zktr.Personnel.model.pojos.PerTngXks;
import cn.zktr.Personnel.model.service.tantiantian.SlySetService;
import cn.zktr.Personnel.model.service.tantiantian.TngPlanService;
import cn.zktr.Personnel.model.service.tantiantian.TngPojService;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.uitls.Homapape;
import cn.zktr.Personnel.model.uitls.ZhijiVo;

@Controller
public class TngProjectController {
	@Autowired 
	private TngPojService proser;
	@Autowired 
	private TngPlanService planser;
	
	/**
	 * 员工
	 */
	@Autowired
	private findService findser;
	

	/**
	 * 薪资套帐
	 */
	@Autowired
	private SlySetService slyser;
	//新增培训项目AddTrainingProject
	@RequestMapping("ttt/add-training-project")
	public void AddTrainingProject(){
	
	}
	@RequestMapping("Profect_yuangongs")
	@ResponseBody
	public Map<String,Object> Profect_yuangongs(Integer page,Integer rows){
		Map<String,Object> map=new HashMap<>();
		map.put("total",proser.seleProjectYuan().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray seleProjectYuan = proser.seleProjectYuan();
		map.put("rows",seleProjectYuan);
		return map;
		
	}
	@RequestMapping("TrainingProjectables")
	@ResponseBody
	public Map<String,Object> TrainingProjectables(Integer page,Integer rows){
		Map<String,Object> map=new HashMap<>();
		map.put("total",proser.seleProAll().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray Project = proser.seleProAll();
		map.put("rows",Project);
		return map;
		
	}
	@RequestMapping("Profect_zhijiEmp")
	@ResponseBody
	public List<ZhijiVo>  Profect_zhijiEmp(){
		List<ZhijiVo>  zhiji=proser.selezhiEmp();
		return zhiji;
		
	}
	@RequestMapping("Add_projects")
	@ResponseBody
	public void  Add_projects(
			PerTngPojs pojs,
			PerTngPlan plan,
			String empid[],
			String ClassId[],
			String planninds,
			String planmaxds,
			HttpSession session,
			PerTngXes xes,
			PerTngXks xks
			){
		//新增培训系项目
		proser.AddTngpojs(plan,planninds,planmaxds,session);
		proser.addPojsy(pojs, session);
		PerTngPojs pojslimi1 = proser.selepojslimt();
		
		//项目人员表
		for (int i=0;i<empid.length-1;i++) {
			PerPmsEmp Empids = slyser.seleEmpids(Integer.parseInt(empid[i]));
			proser.AddTngXes(pojslimi1,Empids);
			proser.AddInt(proser.selexeslimt(),Empids);
			proser.AddTngFeb(proser.seleitnlimt(),"0");
			//新增结果信息表
			PerTngRet ret=new PerTngRet();
			ret.setPerPmsZCl(proser.sele_cls_BYrus(proser.seleTngrusId(Integer.parseInt(ClassId[0])).get().getRusid()));
			ret.setPerTngXe(proser.selexeslimt());
			ret.setRetrem("0");
			proser.addRet(ret);
		}
		//项目课程表
		for (int n=0;n<ClassId.length-1;n++) {
			PerTngRus perTngRus = proser.seleTngrusId(Integer.parseInt(ClassId[n])).get();
			proser.AddTngXks(pojslimi1,perTngRus);
		}
	}
	/**
	 * @param planid
	 * @param empid
	 * @param ClassId新增培训项目，培训计划转培训项目
	 */
	@RequestMapping("adds_pojs")
	@ResponseBody
	public void  adds_pojs(
			String planid,
			String empid[],
			String ClassId[],
			PerTngPojs pojs,
			PerTngXes xes,
			PerTngXks xks,
			HttpSession session) {
		Optional<PerTngPlan> plan = planser.selePlanId(Integer.parseInt(planid));
				//新增培训系项目
				proser.AddTngpojs(plan.get(),pojs,session);
				//项目人员表
				for (int i=0;i<empid.length-1;i++) {
					PerTngPojs pojslimi = proser.selepojslimt();
					PerPmsEmp Empids = slyser.seleEmpids(Integer.parseInt(empid[i]));
					proser.AddTngXes(pojslimi,Empids);
					proser.AddInt(proser.selexeslimt(),Empids);
					//新增feb
					proser.AddTngFeb(proser.seleitnlimt(),"0");
					//新增结果信息表
					PerTngRet ret=new PerTngRet();
					ret.setPerPmsZCl(proser.sele_cls_BYrus(proser.seleTngrusId(Integer.parseInt(ClassId[0])).get().getRusid()));
					ret.setPerTngXe(proser.selexeslimt());
					ret.setRetrem("0");
					proser.addRet(ret);

				}
				//项目课程表
				for (int n=0;n<ClassId.length-1;n++) {
					PerTngPojs pojslimi1 = proser.selepojslimt();
					PerTngRus perTngRus = proser.seleTngrusId(Integer.parseInt(ClassId[n])).get();
					proser.AddTngXks(pojslimi1,perTngRus);
				}
				
	}
	/**
	 * @param id
	 * @param session培训计划转培训项目
	 */
	@RequestMapping("updatePlan")
	@ResponseBody
	public void  updatePlan(String id,	HttpSession session) {
		Optional<PerTngPlan> PlanAll = planser.selePlanId(Integer.parseInt(id));
		session.setAttribute("PlanAll", PlanAll.get());
	}
	@RequestMapping("remosession")
	@ResponseBody
	public void  remosession(HttpSession session) {
		session.removeAttribute("PlanAll");
		
	}
//项目员工pojs_yuangong
	@RequestMapping("pojs_yuangong")
	@ResponseBody
	public Map<String, Object>  pojs_yuangong(Integer pojsid,Integer page,Integer rows) {
		System.err.println(pojsid);
		return proser.pojs_yuangong(pojsid,page,rows);
		
	}
	//项目课程pojs_kecheng
	@RequestMapping("pojs_kecheng")
	@ResponseBody
	public Map<String, Object>  pojs_kecheng(Integer pojsid,Integer page,Integer rows) {
		return proser.pojs_kecheng(pojsid,page,rows);
		
	}
	//查询所有培训结果sele_ret
	@RequestMapping("TrainingResulttables")
	@ResponseBody
	public Map<String, Object>  TrainingResulttables(Integer page,Integer rows) {
		Map<String,Object> map=new HashMap<>();
		map.put("total",proser.sele_ret().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray seleProjectYuan = proser.sele_ret();
		map.put("rows",seleProjectYuan);
		return map;
		
	}
	//根据结果id通过
	@RequestMapping("rus_jieguo")
	@ResponseBody
	public void  rus_jieguo(Integer retid) {
		 proser.tongguo(retid);
		
	}
	//根据结果id不通过
	@RequestMapping("rus_butongguo")
	@ResponseBody
	public void  rus_butongguo(Integer retid) {
		 proser.rus_butongguo(retid);
		
	}
	/**
	 * @param empid
	 * @return根据登录员工id查询培训记录
	 */
	@RequestMapping("gere_peixun")
	@ResponseBody
	public Map<String, Object> gere_peixun(Integer page,Integer rows,HttpSession session) {
		Map<String,Object> map=new HashMap<>();
		map.put("total",proser.gere_peixun(Integer.valueOf(session.getAttribute("id")+"")).size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray seleProjectYuan = proser.gere_peixun(Integer.valueOf(session.getAttribute("id")+""));
		map.put("rows",seleProjectYuan);
		return map;
		//return proser.gere_peixun(empid);
	}
	/**
	 * @param empid
	 * @return查询个人项目结果
	 */
	@RequestMapping("xiangmuJieguo_Byempid")
	@ResponseBody
	public Map<String, Object> xiangmuJieguo_Byempid(Integer page,Integer rows,HttpSession session) {
		return proser.xiangmuJieguo_Byempid(page, rows, session);	
	}
	/**
	 * 查询所有培训记录
	 */
	@RequestMapping("jilu_all")
	@ResponseBody
	public Map<String, Object> peixunjiluAll(Integer page,Integer rows) {
		return proser.peixunjiluAll(page, rows);
		
	}
	/**
	 * 根据姓名查询培训记录
	 */
	Map<String, Object> rust;
	@RequestMapping("rus_rust_name")
	@ResponseBody
	public int rus_rust_name(String empname) {
		rust= proser.peixunjilu_name(empname);
		return proser.peixunjilu_name(empname).size();
		
	}
	@RequestMapping("rus_rust_name_table")
	@ResponseBody
	public Map<String, Object> rus_rust_name_table() {
		return rust;
	}
	/**
	 * 根据姓名查询培训记录
	 */
	Map<String, Object> TrainingResulttable_cha;
	@RequestMapping("TrainingResulttable_cha")
	@ResponseBody
	public int TrainingResulttable_cha(String empname) {
		System.err.println(1111);
		TrainingResulttable_cha= proser.TrainingResulttable_cha(empname);
		return proser.TrainingResulttable_cha(empname).size();
		
	}
	@RequestMapping("TrainingResulttable_name")
	@ResponseBody
	public Map<String, Object> TrainingResulttable_name() {
		return TrainingResulttable_cha;
	}
	
	//反馈状态修改fankuixiugai_
	@RequestMapping("fankuixiugai_s")
	@ResponseBody
	public void fankuixiugai_() throws ParseException {
			proser.fankuixiugai_();

	}
	//根据计划名称查询TrainingProjectables_name
	@RequestMapping("TrainingProjectables_name")
	@ResponseBody
	public JSONArray TrainingProjectables_name(String text){
		System.err.println(text);
			return proser.TrainingProjectables_name(text);

	}
}
